package com.jh.review.service.impl;

import com.jh.review.dao.TblFormMapper;
import com.jh.review.service.IHttpservice;
import com.jh.review.service.IReviewService;
import com.jh.review.util.JsonUtil;
import com.jh.review.util.ReadPropertiesUtils;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @description:
 * @author: guoc
 * @createDate: 2020/7/24 13:11
 */
@Service
@Transactional
@SuppressWarnings("ALL")
public class ReviewService implements IReviewService {


    @Autowired
    IHttpservice httpservice;

    @Autowired
    TblFormMapper tblFormMapper;

    /**
     * 远程调用接口
     * @return java.lang.String
     * @author guoc
     * @date 2020/7/24 15:52
     */
    @Override
    public String httpService(String formId) {
        String url = ReadPropertiesUtils.getConfig("url");
        String paramStr = ReadPropertiesUtils.getConfig("param");
        if (StringUtils.isBlank(url)||StringUtils.isBlank(paramStr)){
            return "本地配置文件url或paramStr未定义";
        }else {
            String assemblyparam = this.assemblyparam(paramStr, formId);
            String retnrnStr = httpservice.httpClientJsonTest(url, assemblyparam);
            if (StringUtils.isNoneBlank(retnrnStr)){
                //formid错误报500情况
                boolean jsonObject = JsonUtil.isJsonObject(retnrnStr);
                if (!jsonObject){
                    return retnrnStr;
                }
                JSONObject returnJSON = JSONObject.fromObject(retnrnStr);
                int success = (int)returnJSON.get("success");
                if (success==0){
                    //调用200，返回为seccess:0
                    return (String) returnJSON.get("mes");
                }
                //成功
                return "SUCCESS";
            }
            return "远程调用失败";
        }
    }

    /**
     * 查询所有手机表单
     * @return java.util.List<java.lang.String>
     * @author guoc
     * @date 2020/7/24 15:53
     */
    @Override
    public List<String> getAllMobileFormId() {
        List<String> allMobileFormId = tblFormMapper.getAllMobileFormId();
        if (allMobileFormId.size()>0){
            return allMobileFormId;
        }
        return null;
    }

    /**
     * 自动请求服务
     * @return void
     * @author guoc
     * @date 2020/7/24 15:53
     */
    @Override
    public String autoHttpService() {
        List<String> allMobileFormId = this.getAllMobileFormId();
        if (allMobileFormId!=null&& allMobileFormId.size()>0){
            Map<String,Object> returnMap=new HashMap<>();
            for (String formId : allMobileFormId) {
                String retnrnStr = this.httpService(formId);
                if (StringUtils.isNoneBlank(retnrnStr)){
                    if ("SUCCESS".equals(retnrnStr)){
                        continue;
                    }
                    returnMap.put(formId,retnrnStr);
                }else {
                    returnMap.put(formId,"远程调用失败");
                }
            }
            if (returnMap.size()>0){
                Map<String, Object> stringObjectMap = this.removeFormId(returnMap);
                return stringObjectMap.toString();
            }else {
                return "手机表单接口无异常";
            }
        }
        return "查询表单失败";
    }

    @Override
    public Map<String, Object> removeFormId(Map<String, Object> returnMap) {
        List<String> formIdMengdian = tblFormMapper.getFormIdMengdian();
        Iterator<String> iterator = returnMap.keySet().iterator();
        while (iterator.hasNext()){
            String key = iterator.next();
            boolean contains = formIdMengdian.contains(key);
            if (contains){
                iterator.remove();
            }
        }
        return returnMap;
    }

    /**
     * 组装参数
     * @return java.lang.String
     * @author guoc
     * @date 2020/7/24 15:53
     */
    private String assemblyparam(String paramStr,String formId){
        JSONObject paramJson = JSONObject.fromObject(paramStr);
        paramJson.put("formId",formId);
        return paramJson.toString();
    }


}
